<?php

/**
 * @author gray.guo
 */
namespace bc\controller;

use yii\web\Controller;
use Yii;

class BaseController extends Controller
{
	/**
	 * 表单重复验证
	 * 
	 * @var false|true
	 */
	public $enableCsrfValidation = false;
	
	/**
	 * @inheritdoc
	 */
	public function actions()
	{
		return [ 
				'error' => [ 
						'class' => 'yii\web\ErrorAction' 
				] 
		];
	}
	
	/**
	 * get请求参数
	 * 
	 * @author gray.guo
	 * @param string $name        	
	 * @param mixed $defaultValue        	
	 * @return array|mixed
	 */
	public function get($name, $defaultValue = NULL)
	{
		return trim(Yii::$app->request->get ( $name, $defaultValue ));
	}
	
	/**
	 * post请求参数
	 * 
	 * @author gray.guo
	 * @param string $name        	
	 * @param mixed $defaultValue        	
	 * @return array|mixed
	 */
	public function post($name=null, $defaultValue = NULL)
	{
		if(empty($name)){
			return Yii::$app->request->post ( $name, $defaultValue );
		}
		return trim(Yii::$app->request->post ( $name, $defaultValue ));
	}
	
	/**
	 *
	 * @author gray.guo
	 * @return yii\web\Request
	 */
	public function getRequest()
	{
		return Yii::$app->request;
	}
	
	/**
	 *
	 * @author gray.guo
	 * @return yii\web\Response
	 */
	public function response()
	{
		return Yii::$app->response;
	}
	
	/**
	 *
	 * @return array
	 */
	public function getParams()
	{
		return Yii::$app->request->getQueryParams ();
	}
	
	/**
	 *
	 * @param string $name        	
	 * @param string $defaultValue        	
	 * @return string
	 */
	public function getParam($name, $defaultValue = NULL)
	{
		return Yii::$app->request->getQueryParam ( $name, $defaultValue = NULL );
	}
	
	/**
	 *
	 * @param string $url        	
	 * @param array $params        	
	 */
	public function forward( $params, $url = '/index/goto')
	{
		Yii::$app->getSession()->setFlash('goto', $params);
		$this->redirect ( $url, '301' );
	}
	
	/**
	 *
	 * @author gray.guo
	 * @param array $data        	
	 * @param number $code        	
	 */
	final public function output($data, $code = 200, $msg = null)
	{
		$response ['code'] = $code;
		$response ['data'] = $data;
		if (! is_null ( $msg ) && is_string ( $msg ))
			$response ['msg'] = $msg;
		echo json_encode ( $response );
		exit ();
	}
	
	
	/**
	 *
	 * @author gray.guo
	 * @param mixed $data        	
	 * @return mixed
	 */
	final public function printr($data, $termination = false)
	{
		echo '<pre>';
		print_r ( $data );
		echo '</pre>';
		if ($termination === true)
			exit ();
	}
	
	/**
	 * 
	 * @param string $data
	 */
	final public function filter($data)
	{
		return trim($data);
	}
	
	
	/**
	 * 
	 * @param string $methodType
	 * 输出保存格式化数据
	 */
	final public function formatSave($methodType = 'post')
	{
		
		if($methodType == 'post')
		{
			foreach ($_POST as $key=>$item)
			{
				echo "\$data['{$key}'] = \$this->post('{$key}');<br/>";
			}
		}
		else 
		{
			foreach ($_GET as $key=>$item)
			{
				echo "\$data['{$key}'] = \$this->get('{$key}');<br/>";
			}
		}
		
		exit;
	}
	
	/**
	 * 获取当前域名
	 * @return string
	 */
	final public function getBaseUrl()
	{
		return Yii::$app->request->hostInfo;
	}
	
	/**
	 * @return bc\util\Wechat
	 */
	final public function wechat()
	{
		return Yii::$app->wechat;
	}
	
	/**
	 * 获取完整url（含host以及参数）
	 * @return string
	 */
	final public function getFullUrl()
	{
		return Yii::$app->request->getHostInfo().Yii::$app->request->url;
	}

	/**
	 * 浏览器友好的变量输出
	 * @param mixed $var 变量
	 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
	 * @param string $label 标签 默认为空
	 * @param boolean $strict 是否严谨 默认为true
	 * @return void|string
	 */
	final static function dump($var, $echo=true, $label=null, $strict=true) {
		$label = ($label === null) ? '' : rtrim($label) . ' ';
		if (!$strict) {
			if (ini_get('html_errors')) {
				$output = print_r($var, true);
				$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
			} else {
				$output = $label . print_r($var, true);
			}
		} else {
			ob_start();
			var_dump($var);
			$output = ob_get_clean();
			if (!extension_loaded('xdebug')) {
				$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
				$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
			}
		}
		if ($echo) {
			echo($output);
			return null;
		}else
			return $output;
	}

}